#include "serial.h"
#include "main.h"
#include "usart.h"

#include <string.h>


int fputc(int ch, FILE *fp)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
  return ch;
}


int fgetc(FILE *fp)
{
  uint8_t ch = 0;
  HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
  return ch;
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if(huart->Instance == USART1)
	{
		HAL_UART_DMAStop(huart);
		
		int cnt =100 - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
		
		HAL_UART_Transmit(&huart1,rx_buf,cnt,5000);
		memset(rx_buf,0,100);
		HAL_UARTEx_ReceiveToIdle_DMA(&huart1,rx_buf,100);
		__HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
	}
	
}

